package com.wjb.xietong.ota.tab4;

import java.util.Calendar;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.wjb.xietong.R;
import com.wjb.xietong.component.AppGlobal;
import com.wjb.xietong.control.WJBControl;
import com.wjb.xietong.ota.BaseFM;
import com.wjb.xietong.ota.login.OTALogin;
import com.wjb.xietong.server.IRequestResultListener;
import com.wjb.xietong.server.login.LoginParam;
import com.wjb.xietong.util.PreferencesService;
import com.wjb.xietong.util.UpdateManager;

public class FMSetting extends BaseFM {

	private Dialog dialog;

	private LinearLayout btn_systemsetting;
	private LinearLayout btn_suggestion;
	private LinearLayout btn_score;
	private LinearLayout btn_checkversion;
	private LinearLayout btn_help;
	private LinearLayout btn_about;
	private Button btn_loginout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		setContentField(inflater.inflate(R.layout.page_setting, null));
		setTopBarAndAction("设置", null, null);
		return view;

	}

	@Override
	protected void findView(View v) {
		// TODO Auto-generated method stub
		btn_systemsetting = (LinearLayout) v
				.findViewById(R.id.btn_systemsettting);
		btn_suggestion = (LinearLayout) v.findViewById(R.id.btn_suggestion);
		btn_score = (LinearLayout) v.findViewById(R.id.btn_score);
		btn_checkversion = (LinearLayout) v
				.findViewById(R.id.btn_checknewversion);
		btn_help = (LinearLayout) v.findViewById(R.id.btn_help);
		btn_about = (LinearLayout) v.findViewById(R.id.btn_about);
		btn_loginout = (Button) v.findViewById(R.id.btn_loginout);
	}

	@Override
	protected void setOnClickEvent() {
		// TODO Auto-generated method stub
		super.setOnClickEvent();
		btn_systemsetting.setOnClickListener(btnClickListener);
		btn_suggestion.setOnClickListener(btnClickListener);
		btn_score.setOnClickListener(btnClickListener);
		btn_checkversion.setOnClickListener(btnClickListener);
		btn_help.setOnClickListener(btnClickListener);
		btn_about.setOnClickListener(btnClickListener);
		btn_loginout.setOnClickListener(btnClickListener);
	}

	OnClickListener btnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = null;
			switch (v.getId()) {
			case R.id.btn_systemsettting:
				intent = new Intent(getActivity(), OTASystemSetting.class);
				break;
			case R.id.btn_suggestion:
				intent = new Intent(getActivity(), OTASuggestion.class);
				break;
			case R.id.btn_score:
				Uri uri = Uri.parse("market://details?id="
						+ FMSetting.class.getPackage().getName());
				intent = new Intent(Intent.ACTION_VIEW, uri);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				break;
			case R.id.btn_checknewversion:
				showNewVersionDialog();
				break;
			case R.id.btn_help:
				intent = new Intent(getActivity(), OTAHelpWebView.class);
				break;
			case R.id.btn_about:
				intent = new Intent(getActivity(), OTAAbout.class);
				break;
			case R.id.btn_loginout:
				showLoginOutDialog();
				break;
			default:
				break;
			}
			if (intent != null) {
				startActivity(intent);
			}
		}
	};
	
	

	private void showLoginOutDialog() {
		dialog = new Dialog(getActivity(), R.style.CustomDialog);
		dialog.setContentView(R.layout.page_login_out_dialog);
		Button btnConfirm = (Button) dialog.findViewById(R.id.btn_close);
		Button btnClose = (Button) dialog.findViewById(R.id.btn_continue);
		btnConfirm.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
				loginOut();
			}
		});
		btnClose.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
	}

	/**
	 * 登出
	 */
	private int retryTime = 0;

	private void loginOut() {
		final PreferencesService preferences = new PreferencesService(AppGlobal
				.getInstance().getBaseContext());
		final String userName = preferences
				.getLoginUserInfoPreference(PreferencesService.PREFERENCE_KEY_USER_NAME);
		final String userPwd = preferences
				.getLoginUserInfoPreference(PreferencesService.PREFERENCE_KEY_USER_PASSWORD);
		if (null == userName || "".equals(userName) || null == userPwd
				|| "".equals(userPwd)) {
			return;
		}
		preferences.saveUserLogin("no");
		 
		LoginParam param = new LoginParam();
		param.setUserName(userName);
		param.setUserPwd(userPwd);
		param.setClientId("");// 此处为了清除服务器上的clientId保证该用户登出以后不能接受到通知
		WJBControl.requestLogin(Calendar.getInstance().getTimeInMillis(),
				param, new IRequestResultListener() {

					@Override
					public void requestSuccess(long requestUniqueID) {
						preferences.removeLoginUserInfo();// 清除已保存的用户登录信息
						app_.getActManager().popAllActivityExceptOne(
								OTALogin.class);
					}

					@Override
					public void requestFaield(long requestUniqueID,
							String errCode, String errMsg) {
						if (retryTime < 5) {// 重试5次
							loginOut();
							retryTime++;
						}
					}
				});
	}

	private void showNewVersionDialog() {
		// 请求成功
		if (true) {
			final String serverVersion = "0.0.0";
			final String appDownloadPath = "http://www.wjb.com";
			UpdateManager updateManager = new UpdateManager(AppGlobal
					.getInstance().getContext(), AppGlobal.getInstance()
					.getLocalAppVersion(), serverVersion, appDownloadPath);
			if (updateManager.checkVersion()) {
				updateManager.showNoticeDialog();
			} else {
				dialog = new Dialog(getActivity(), R.style.CustomDialog);
				dialog.setContentView(R.layout.page_setting_new_version_dialog);
				Button btnConfirm = (Button) dialog
						.findViewById(R.id.btn_close);
				btnConfirm.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
				dialog.setCanceledOnTouchOutside(false);
				dialog.show();
			}
		} else {// 请求失败
			Toast.makeText(getActivity(), "获取新版本失败，请重试...", Toast.LENGTH_LONG)
					.show();
		}
	}
}
